package com.szqhyxj.youxiangju.biz.merchantHome.plan;

import android.util.Log;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.biz.plan.PlanList;
import com.szqhyxj.youxiangju.biz.plan.PlanModel;
import com.szqhyxj.youxiangju.biz.plan.PlanStatus;
import com.szqhyxj.youxiangju.biz.plan.RowItem;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class PlanListPresenter extends AbBasePresenter implements PlanListContract.Presenter {

    private final PlanListContract.View view;
    private PlanModel.QueryPlanEntity entity;

    public PlanListPresenter(PlanListContract.View view) {
        this.view = view;
        entity = new PlanModel.QueryPlanEntity();
        entity.setPageSize(50);
        entity.setPageNumber(1);
    }

    @Override
    public void init(int type) {
        //执行中
        if (type == PlanStatus.RUNNING.getCode()) {
            initRunning();
        }
        //暂停
        if (type == PlanStatus.PAUSED.getCode()) {
            initPaused();
        }
        //失败
        if (type == PlanStatus.FAIL.getCode()) {
            initFail();
        }
        //完成
        if (type == PlanStatus.SUCCESS.getCode()) {
            initSuccess();
        }
    }

    @Override
    public void initRunning() {
        addDisposables(PlanModel.getPlanLists(1)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<RowItem>>() {
                    @Override
                    protected void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }

                    @Override
                    protected void successAccept(List<RowItem> rowItems) {
                        view.dismissLoading();
                        view.showPlanList(rowItems);
                        Log.e("正在执行计划的", rowItems.size() + "");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));
        /*addDisposables(PlanModel.getPlanList(entity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<PlanList>() {
                    @Override
                    protected void successAccept(PlanList planList) {
                        view.dismissLoading();
                        view.showPlanList(planList);
                    }

                    @Override
                    public void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));*/
    }

    @Override
    public void initPaused() {
        /*addDisposables(PlanModel.getPlanListPaused(entity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<PlanList>() {
                    @Override
                    protected void successAccept(PlanList planList) {
                        view.dismissLoading();
                        view.showPlanList(planList);
                    }

                    @Override
                    public void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));*/
        addDisposables(PlanModel.getPlanLists(2)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<RowItem>>() {
                    @Override
                    protected void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }

                    @Override
                    protected void successAccept(List<RowItem> rowItems) {
                        view.dismissLoading();
                        view.showPlanList(rowItems);
                        Log.e("暂停计划的", rowItems.size() + "");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));
    }

    @Override
    public void initFail() {
        /*addDisposables(PlanModel.getPlanListFailure(entity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<PlanList>() {
                    @Override
                    protected void successAccept(PlanList planList) {
                        view.dismissLoading();
                        view.showPlanList(planList);
                        view.showRefresh(false);
                    }

                    @Override
                    public void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));*/
        addDisposables(PlanModel.getPlanLists(3)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<RowItem>>() {
                    @Override
                    protected void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }

                    @Override
                    protected void successAccept(List<RowItem> rowItems) {
                        view.dismissLoading();
                        view.showPlanList(rowItems);
                        Log.e("异常结束的", rowItems.size() + "");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));
    }

    @Override
    public void initSuccess() {
        /*addDisposables(PlanModel.getPlanListSuccess(entity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<PlanList>() {
                    @Override
                    protected void successAccept(PlanList planList) {
                        view.dismissLoading();
                        view.showPlanList(planList);
                    }

                    @Override
                    public void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));*/
        addDisposables(PlanModel.getPlanLists(4)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<List<RowItem>>() {
                    @Override
                    protected void error(String code, String msg) {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }

                    @Override
                    protected void successAccept(List<RowItem> rowItems) {
                        view.dismissLoading();
                        view.showPlanList(rowItems);
                        Log.e("正常结束的", rowItems.size() + "");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showRefresh(false);
                    }
                }));
    }

    @Override
    public void setNormalPaln() {
        view.showNormalPaln();
    }

    @Override
    public void setErrPlan() {
        view.showErrPlan();
    }

    @Override
    public void addDisposables(Disposable disposable) {
        super.addDisposables(disposable);
        view.showLoading();
    }
}